<?php

namespace app\common\model\goods;

use think\Model;
use app\common\model\purchase\EnterGoods;
use app\common\model\goods\GoodsHouseStock;

class Goods extends Model
{

    

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['normal' => __('上架'), 'hidden' => __('下架')];
    }
    public function getMaterialList()
    {
        return [0 => __('非物料'), 1 => __('物料')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    //获取最近平均价
    public function getfinal($goods_id,$storehouse_id){


        $where = array('goods_id'=>$goods_id,'storehouse_id'=>$storehouse_id);
        $GoodsHouseStock = new GoodsHouseStock;
        $row = $GoodsHouseStock->where($where)->find();
        return isset($row['weighting_cost'])?$row['weighting_cost']:0;
        return isset($row['avg_cost'])?$row['avg_cost']:0;

        if(empty($storehouse_id)){
            $where = array('goods_id'=>$goods_id,'final_cost'=>array('>',0));
        }else{
            $where = array('goods_id'=>$goods_id,'storehouse_id'=>$storehouse_id,'final_cost'=>array('>',0));
        }
        $EnterGoods = new EnterGoods;
        $final_cost = $EnterGoods->where($where)->limit(0,3)->avg('final_cost');
        return $final_cost;
    }
    //获取最新成本
    public function getcost($goods_id,$storehouse_id){
        $where = array('goods_id'=>$goods_id,'storehouse_id'=>$storehouse_id);
        $GoodsHouseStock = new GoodsHouseStock;
        $row = $GoodsHouseStock->where($where)->find();
        if($row && !empty($row['stock']) && !empty($row['balance'])){
            return bcdiv($row['balance'],$row['stock'],4);
        }else{
            return 0;
        }
        return isset($row['new_cost'])?$row['new_cost']:0;
        return isset($row['new_cost'])?$row['new_cost']:0;

        if(empty($storehouse_id)){
            $where = array('goods_id'=>$goods_id,'final_cost'=>array('>',0));
        }else{
            $where = array('goods_id'=>$goods_id,'storehouse_id'=>$storehouse_id,'final_cost'=>array('>',0));
        }
        $EnterGoods = new EnterGoods;
        $row = $EnterGoods->where($where)->find();
        return isset($row['final_cost'])?$row['final_cost']:0;
    }



    public function cate()
    {
        return $this->belongsTo('app\common\model\goods\Cate', 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function supplier()
    {
        return $this->belongsTo('app\common\model\supplier\Supplier', 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
        return $this->belongsTo('app\common\model\Supplier', 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function brands()
    {
        return $this->belongsTo('app\common\model\supplier\Brands', 'brands_id', 'id', [], 'LEFT')->setEagerlyType(0);
        return $this->belongsTo('app\common\model\Supplier', 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
